<?php

namespace MoChat\App\Sales\Service;

use MoChat\App\Sales\Contract\SalesTaskEmployeeContract;
use MoChat\App\Sales\Model\SalesTaskEmployee;
use MoChat\Framework\Service\AbstractService;

class SalesTaskEmployeeService extends AbstractService implements SalesTaskEmployeeContract
{
    /**
     * @var SalesTaskEmployee
     */
    protected $model;

    public function createSalesTaskEmployee($data): bool
    {
        return $this->model->createAll($data);
    }

    public function getTotalSalesTaskBySalesTaskId($salesTaskId)
    {
        return $this->model->where('sales_task_id', $salesTaskId)->sum('sales_task');
    }

    public function getSalesTaskEmployeeBySalesTaskId($salesTaskId): array
    {
        $list = $this->model->where('sales_task_id', $salesTaskId)->pluck('sales_task', 'work_employee_id');
        return empty($list) ? [] : $list->toArray();
    }

    public function updateSalesTaskEmployee($where, $number, $data): int
    {
        return $this->model->where($where)->increment('sales_task', $number, $data);
    }

    public function getListSalesTaskEmployee($where, $filed = ['*']): array
    {
        return $this->model->getPageList($where, $filed);
    }

    public function showSalesClientTaskEmployee($id, $filed = ['*']): array
    {
        return $this->model->getOneById($id, $filed);
    }

    public function getTotalByCorpIdd($corpId,$startTime,$endTime)
    {
        return $this->model->where('corp_id', $corpId)
            ->where('created_at','>=',$startTime)
            ->where('created_at','<',$endTime)
            ->sum('completed');
    }

    public function getSalesTaskBySalesTaskId($salesTaskId): int
    {
        return $this->model->where('sales_task_id', $salesTaskId)->count();
    }
}